<?php

/**
 * other actions.
 *
 * @package    iEasy
 * @subpackage other
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class otherActions extends sfActions {
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        //$this->forward('default', 'module');
    }

    public function executeJQuery(sfWebRequest $request) {
        switch ($request->getParameter("submitFlag"))
        {
            case "contentMenu":
                return $this->renderComponent("other", "getContentMenu");
                break;
            case "relatedSearch":
                if ($request->getParameter("whereStr") == null)
                    return $this->renderComponent("news", "getSearchByTag");
                else
                    return $this->renderComponent("news", "getSearchByTag", array("whereStr" => urldecode($request->getParameter("whereStr")), "searchType" => $request->getParameter("searchType")));
                break;
            case "login":
                 $userName =$request->getParameter("userName");
                 $pwd = $request->getParameter("pwd");
                 if($userName == null || $pwd == null)
                     return $this->renderText("no");
                 else
                     return $this->renderText($this->login($userName,$pwd));
                break;
            case "logout":
                return $this->renderText($this->logout());
                break;
        }
    }

    public function executeFocus(sfWebRequest $request) {
        $this->uploadType = "images";
        if ($request->getParameter("uploadType") != null)
            $this->uploadType = $request->getParameter("uploadType");
        $this->focus = new FocusContent();
        $id = $request->getParameter("id");
        $this->type = $request->getParameter("contentTypeId");
        $this->catalogId = $request->getParameter("catalogId");
        //$fileControlName = $$request->getParameter("fileControlName");
        if (is_null($id) == false && is_null($this->type) == false && is_null($this->catalogId) == false) {
            $this->focus = Doctrine_Core::getTable("FocusContent")
                    ->createQuery()
                    ->where("content_id = ?", $id)
                    ->andWhere("content_type_i= ?", $this->type)
                    ->andWhere("catalog_id= ?", $this->catalogId)
                    ->execute();
        }
        else
        {
            if (is_null($id) == true && is_null($request->getPostParameter("txtCatalogId")) == false && is_null($request->getPostParameter("txtContentTypeId")) == false) {

                $uploadType = $request->getParameter("txtUploadType");
                $desFilePath = utility::uploadFile($uploadType, "txtLocalFile");
                $focus = new FocusContent();
                $focus->setCatalogId($this->catalogId);
                $focus->setContentTypeI($this->type);

                $focus->setSubTitle($request->getParameter("txeTitle"));
                $focus->setIsOnTopB($request->getParameter("isTop"));
                $focus->setShortWord($request->getParameter("txtBrief"));
                $focus->setCreateTime(date("Y-m-d H:i:s"));
                $focus->setModifyTime($focus->getCreateTime());
                $focus->setFocusPhoto("/" . $desFilePath);
                $focus->save();
                return $this->renderText("<script type=\"text/javascript\" language=\"javascript\">window.opener=null;window.open('','_self');window.returnValue='" . $focus->getFocusContentId() . "'; alert('提交成功！'); window.close();</script>");
            }
        }
    }

    function executeTest(sfWebRequest $request) {
        $this->body = "";
        if ($request->getParameter("txtBody") != nuLL)
            $this->body = $request->getParameter("txtBody");
    }
    
    function executeSurveyList(sfWebRequest $request) {
      $this->recordCount=$request->getParameter('recordCount', 10);
      $this->currentPage=$request->getParameter('currentPage', 1);

      $query = Doctrine_Query::create()->from("OnlineSurvey");
      $query=$query->orderBy('is_on_top_b desc');
      $this->pages = new sfDoctrinePager('OnlineSurvey', $this->currentPage);//$this->currentPage
      $this->pages->setQuery($query);  //$q   数据库查询语句
      $this->pages->setPage($this->currentPage);//
      $this->pages->init();
    }

    public function executeMusicbox(sfWebRequest $request) {
        $execSql = $request->getParameter("musicEdit");
        if (1 == $execSql) {
            $Ids = $request->getParameter("contents");
            $Id = explode(" ", $Ids);
            for ($i = 0; $i < count($Id); $i++)
            {
                if (!empty($Id[$i])) {
                    $boxrecord = @Doctrine_Core::getTable('boxrecord')->find($Id[$i]);
                    $boxrecord->setImage($request->getParameter("Txt" . $boxrecord->getBoxRecordId()));
                    $boxrecord->setLink($request->getParameter("link" . $boxrecord->getBoxRecordId()));
                    $boxrecord->setModifyTime(date("Y-m-d H:i:s"));
                    $boxrecord -> save();
                }
            }
        }
        $this->boxRecords = @Doctrine_Core::getTable('boxrecord')->createQuery()->limit(4)->execute();
    }

    public function executeMoviebox(sfWebRequest $request) {
        $execSql = $request->getParameter("movieEdit");
        if (1 == $execSql) {
            $Ids = $request->getParameter("contents");
            $Id = explode(" ", $Ids);
            for ($i = 0; $i < count($Id); $i++)
            {
                if (!empty($Id[$i])) {
                    $boxrecord = @Doctrine_Core::getTable('boxrecord')->find($Id[$i]);
                    $boxrecord->setImage($request->getParameter("Txt" . $boxrecord->getBoxRecordId()));
                    $boxrecord->setLink($request->getParameter("link" . $boxrecord->getBoxRecordId()));
                    $boxrecord->setModifyTime(date("Y-m-d H:i:s"));
                    $boxrecord -> save();
                }
            }
        }
        $this->boxRecords = @Doctrine_Core::getTable('boxrecord')->createQuery()->limit(4)->orderBy('box_record_id desc')->execute();

    }

    public function executeAdvertise(sfWebRequest $request) {
        $execSql = $request->getParameter("adverEdit");
        if (1 == $execSql) {
            $Ids = $request->getParameter("contents");
            $Id = explode(" ", $Ids);
            for ($i = 0; $i < count($Id); $i++)
            {
                if (!empty($Id[$i])) {
                    $advertise = @Doctrine_Core::getTable('Advertise')->find($Id[$i]);
                    $advertise->setImage($request->getParameter("Txt" . $advertise->getAdvertiseId()));
                    $advertise->setLink($request->getParameter("link" . $advertise->getAdvertiseId()));
                    $advertise->setModifyTime(date("Y-m-d H:i:s"));
                    $advertise -> save();
                }
            }
        }
        $this->advertises = @Doctrine_Core::getTable('Advertise')->createQuery()->execute();

    }

    function login($userName, $pwd) {
        $users = Doctrine_Core::getTable("SystemUser")
                ->createQuery()
                ->where("name=?", $userName)
                ->andWhere("password=?", $pwd)
                ->execute();

        foreach ($users as $currentUser)
        {
            $this->getUser()->setAttribute("adminUserName", $currentUser->getName());
            $this->getUser()->setAttribute("adminUserId", $currentUser->getSystemUserId());
            //$this->getUser()->setAttribute("IsAuthorized", 1);
            //session_start();
            $_SESSION['IsAuthorized'] = 1;;
            $currentUser->setIsOnlineB("1");
            $currentUser->save();
            return "ok";
        }
        return "no";
    }

    function logout() {
       $users = Doctrine_Core::getTable("SystemUser")
                ->createQuery()
                ->where("name=?", $this->getUser()->getAttribute("adminUserName"))
                ->execute();
        $this->getUser()->setAttribute("adminUserName", null);
        $this->getUser()->setAttribute("adminUserId", null);
        session_start();
        $_SESSION['IsAuthorized'] = null;
        foreach ($users as $currentUser)
        {
            $currentUser->setIsOnlineB("0");
            $currentUser->save();
            return "ok";
        }
        return "no";
    }
}
